package com.googlecode.spoluyan.google.googleplus;

import java.util.HashMap;
import java.util.Map;

import com.googlecode.spoluyan.google.common.AbstractGoogleAPIClient;
import com.googlecode.spoluyan.google.common.Defines;
import com.googlecode.spoluyan.google.common.Utils;
import com.googlecode.spoluyan.google.common.Defines.GooglePlusDefines.ActivityCollection;
import com.googlecode.spoluyan.google.common.Defines.GooglePlusDefines.OrderBy;
import com.googlecode.spoluyan.google.common.Defines.GooglePlusDefines.PersonCollection;
import com.googlecode.spoluyan.google.common.Defines.GooglePlusDefines.SortOrder;
import com.googlecode.spoluyan.google.googleplus.data.Activity;
import com.googlecode.spoluyan.google.googleplus.data.ActivitySearchResult;
import com.googlecode.spoluyan.google.googleplus.data.Comment;
import com.googlecode.spoluyan.google.googleplus.data.CommentSearchResult;
import com.googlecode.spoluyan.google.googleplus.data.PeopleSearchResult;
import com.googlecode.spoluyan.google.googleplus.data.Person;

public class GooglePlusClient extends AbstractGoogleAPIClient {

    public GooglePlusClient(String key) {
	super(key);
	this.name = Defines.ClientName.GOOGLE_PLUS;
    }

    public Person getPerson(String userId) {
	if (Utils.isEmptyString(userId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.PEOPLE, Defines.SLASH, userId), Person.class);
    }

    public PeopleSearchResult searchPerson(String query, String language, Integer maxResults, String pageToken) {
	if (Utils.isEmptyString(query)) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	queryParams.put(Defines.GooglePlusDefines.Parameter.QUERY, query);
	if (!Utils.isEmptyString(language)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.LANGUAGE, language);
	}
	if (maxResults != null) {
	    int val = maxResults.intValue();
	    if (val > 0) {
		queryParams.put(Defines.GooglePlusDefines.Parameter.MAX_RESULTS, String.valueOf(val));
	    }
	}
	if (!Utils.isEmptyString(pageToken)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.PAGE_TOKEN, pageToken);
	}
	return Utils.fromJSON(Utils.doRequest(key, queryParams, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.PEOPLE), PeopleSearchResult.class);
    }

    public PeopleSearchResult getPersonListByActivity(String activityId, PersonCollection collection,
	    Integer maxResults, String pageToken) {
	if (Utils.isEmptyString(activityId) || collection == null) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	if (maxResults != null) {
	    int val = maxResults.intValue();
	    if (val > 0) {
		queryParams.put(Defines.GooglePlusDefines.Parameter.MAX_RESULTS, String.valueOf(val));
	    }
	}
	if (!Utils.isEmptyString(pageToken)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.PAGE_TOKEN, pageToken);
	}
	return Utils.fromJSON(Utils.doRequest(key, queryParams, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.ACTIVITIES, Defines.SLASH, activityId, Defines.SLASH,
		Defines.GooglePlusDefines.Path.PEOPLE, Defines.SLASH, collection.getName()), PeopleSearchResult.class);
    }

    public Activity getActivity(String activityId) {
	if (Utils.isEmptyString(activityId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.ACTIVITIES, Defines.SLASH, activityId), Activity.class);
    }

    public ActivitySearchResult getActivitiesList(String userId, ActivityCollection collection, Integer maxResults,
	    String pageToken) {
	if (Utils.isEmptyString(userId) || collection == null) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	if (maxResults != null) {
	    int val = maxResults.intValue();
	    if (val > 0) {
		queryParams.put(Defines.GooglePlusDefines.Parameter.MAX_RESULTS, String.valueOf(val));
	    }
	}
	if (!Utils.isEmptyString(pageToken)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.PAGE_TOKEN, pageToken);
	}
	return Utils.fromJSON(Utils.doRequest(key, queryParams, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.PEOPLE, Defines.SLASH, userId, Defines.SLASH,
		Defines.GooglePlusDefines.Path.ACTIVITIES, Defines.SLASH, collection.getName()),
		ActivitySearchResult.class);
    }

    public ActivitySearchResult searchActivity(String query, String language, Integer maxResults, OrderBy orderBy,
	    String pageToken) {
	if (Utils.isEmptyString(query)) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	queryParams.put(Defines.GooglePlusDefines.Parameter.QUERY, query);
	if (!Utils.isEmptyString(language)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.LANGUAGE, language);
	}
	if (maxResults != null) {
	    int val = maxResults.intValue();
	    if (val > 0) {
		queryParams.put(Defines.GooglePlusDefines.Parameter.MAX_RESULTS, String.valueOf(val));
	    }
	}
	if (orderBy != null) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.ORDER_BY, orderBy.getName());
	}
	if (!Utils.isEmptyString(pageToken)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.PAGE_TOKEN, pageToken);
	}
	return Utils.fromJSON(Utils.doRequest(key, queryParams, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.ACTIVITIES), ActivitySearchResult.class);
    }

    public Comment getComment(String commentId) {
	if (Utils.isEmptyString(commentId)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(key, null, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.COMMENTS, Defines.SLASH, commentId), Comment.class);
    }

    public CommentSearchResult getCommentsList(String activityId, Integer maxResults, SortOrder sortOrder,
	    String pageToken) {
	if (Utils.isEmptyString(activityId)) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	if (maxResults != null) {
	    int val = maxResults.intValue();
	    if (val > 0) {
		queryParams.put(Defines.GooglePlusDefines.Parameter.MAX_RESULTS, String.valueOf(val));
	    }
	}
	if (sortOrder != null) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.SORT_ORDER, sortOrder.getName());
	}
	if (!Utils.isEmptyString(pageToken)) {
	    queryParams.put(Defines.GooglePlusDefines.Parameter.PAGE_TOKEN, pageToken);
	}
	return Utils.fromJSON(Utils.doRequest(key, queryParams, Defines.GooglePlusDefines.API_URL,
		Defines.GooglePlusDefines.Path.ACTIVITIES, Defines.SLASH, activityId, Defines.SLASH,
		Defines.GooglePlusDefines.Path.COMMENTS), CommentSearchResult.class);
    }
}
